# 克里金参数调整测试
import json
import os
import pandas as pd
from config import Config
from utils.dms_convert import dms_to_deg
from utils.interpolation import kriging_batch
from utils.detection import detect_anomalies
from utils.cleaning import clean_precipitation_data
from cenresult.evaluate_interp import evaluate_interpolation


# 确保 result 目录存在
os.makedirs("result", exist_ok=True)

# --- Step1. 读取数据单个文件 ---
# with open("data/test.json", "r", encoding="utf-8") as f:
with open("data/data2025052207.json", "r", encoding="utf-8") as f:
    data = json.load(f)

# --- Step1.1. 数据清理 ---
print(f"数据清理前的记录数量: {len(data)}")
print("进行数据清理（删除降水无效值）...")
data = clean_precipitation_data(data)
print(f"数据清理后的记录数量: {len(data)}")


records = []
for item in data:
    try:
        lon = dms_to_deg(item["Lon"])
        lat = dms_to_deg(item["Lat"]) # 度分秒格式的经纬度
        # lon = float(item["Lon"]) # 度格式的经纬度
        # lat = float(item["Lat"])
        pre = float(item["PRE_1h"])
        records.append({
            "Station_Id_C": item["Station_Id_C"],
            "Station_Name": item["Station_Name"],
            "lon": lon,
            "lat": lat,
            "pre": pre
        })
    except Exception as e:
        print(f"处理记录时出错: {e}")  # 打印错误信息
        continue

df = pd.DataFrame(records)
# 插值操作
print(f"正在 Kriging 插值...")
# df["IDW"] = idw_batch(df)
df["Kriging"], df["Kriging_std"] = kriging_batch(df)


# -------------------
# 异常检测
print(f"进行异常检测...")
df = detect_anomalies(df, Config)

# -------------------
# 读取 station.xlsx 文件
station_df = pd.read_excel("data/stations.xlsx")
# 获取站号列表
valid_station_ids = station_df["站号"].tolist()

# -------------------
# 结果保存
output_fields = [
    "Station_Id_C", "Station_Name", "Region", "lon", "lat", "pre",
    "Kriging", "Kriging_std",
    "Kriging_residual",
    "Kriging_relative_error",
    "Z_value", 
    "异常等级"
]

print(f"站点过滤前的记录数量: {len(df)}")
# 过滤站点，只保留站号在 valid_station_ids 列表中的记录
filtered_df = df[df["Station_Id_C"].isin(valid_station_ids)]
print(f"站点过滤后的记录数量: {len(filtered_df)}")

# 生成结果文件名
excel_file_name = os.path.join("cenresult", f"测量值与估算值.xlsx")
# 输出过滤后的结果到 Excel 文件
filtered_df[output_fields].to_excel(excel_file_name, index=False)


# # 创建包含测量值和估算值的 DataFrame
# measure_estimate_df = filtered_df[["pre", "Kriging","Station_Id_C"]]
# measure_estimate_df.columns = ["测量值", "估算值", "站号"]

# # 将测量值和估算值写入 Excel 文件
# measure_estimate_file = os.path.join("cenresult", "测量值与估算值.xlsx")
# measure_estimate_df.to_excel(measure_estimate_file, index=False)
# print(f"测量值和估算值已写入 {measure_estimate_file}")

# evaluate_interpolation(filtered_df["Kriging"], 
#                        filtered_df["pre"], log_transform=False,
#                        remove_small_obs=False,
#                        small_obs_thresh=Config.SMALL_RAIN_THRESH,
#                        plot=True,
#                        output_path=excel_file_name) 

# -------------------
# 评估结果
print("完成")

